<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">纵信快手项目管理平台</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <i class="el-icon-user-solid"></i>
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on" 
        />
      </el-form-item>
      <el-tooltip
        v-model="capsTooltip"
        content="Caps lock is On"
        placement="right"
        manual
      >
        <el-form-item prop="password">
          <span class="svg-container">
            <i class="el-icon-lock"></i>
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            autocomplete="on"
            
          />
        </el-form-item>
      </el-tooltip>
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click="postLogin"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>
import {setToken} from '../../utils/auth.js'
export default {
  
  data() {
    const validateUsername = (rule, value, callback) => {
      var myname =
        /^([a-zA-Z0-9]+[_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
      if (!myname.test(value)) {
        callback(new Error("请输入正确用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码长度不能小于6位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
      },
      islogin:false,
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      usermsg:""
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        // const query = route.query
        // if (query) {
        //   this.redirect = query.redirect
        //   this.otherQuery = this.getOtherQuery(query)
        // }
      },
      immediate: true,
    },
  },
  mounted(){
    this.islogin = false
  },
  methods: {
    postLogin() {
      // this.$axios.post("/home/login", this.loginForm).then((res)=>{
      //   console.log(res)
      //   this.$router.replace({
      //             path: "/",
      //             query: { userName: res.data.name },
      //           });
      // }).catch((e)=>{
      //   console.log(e);
      // })
      this.$axios
        .post("/api/user/login", this.loginForm)
        .then((e) => {
          // if (!this.loginform["logpass"]) {
          //   this.loginform["password"] = "";
          // }
          // Cookies.set("loginInfo", this.loginform);
          // setToken(e.data.token);
          console.log(e)
         if(e.data.code == 1){
           const {token}=e.data.data
           setToken(token)
           this.$axios.get('/api/user/info',{token:token}).then((res)=>{
            console.log(res)
            this.usermsg = res.data.data
            this.$bus.emit("userInfo",this.usermsg);
            sessionStorage.setItem('userINFO',JSON.stringify(this.usermsg))
           })
          console.log("登录成功", e);
           this.$message({
          message: '登录成功',
          type: 'success'
        });
           this.$router.replace({
            path: "/"
          });
         }
         else if(e.data.code == 0){
          this.$message.error(e.data.msg);
          this.loginForm.password = ""
         }

        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
  },
};
</script>


<style lang="scss">
$bg:#fff;
$light_gray:#333;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255);
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style lang="scss" scoped>
$bg:#292D35;
$dark_gray:#889aa4;
$light_gray:#444;

.login-container {
 height: 100%;
  width: 100%;
  background: url(https://cdn.chiefgr.com/public/D09/cont6-back.png) no-repeat center center $bg;
  background-size: 100% 100%;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 250px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
